//
//  UICategoryUnit.m
//  Dolphin
//
//  Created by Matthew on 11-8-13.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "UICategoryUnit.h"

@implementation UICategoryUnit

@synthesize albumPath;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        
    }
    return self;
}

- (void)   initUnit:(NSString *) link
                    title:(NSString *) inTitle
        albumPath:(NSString *) inAlbumPath
     borderWidth:(int) inWidth
   borderHeight:(int) inHeight
            fontSize:(int) inFontSize
{
    if (inAlbumPath != Nil)
    {
        albumPath = [[NSString alloc] initWithString:inAlbumPath];
    }
    
    borderWidth = inWidth;
    borderHeight = inHeight;
    
    bgView = [[UIView alloc] initWithFrame:self.frame];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    innerImageView = [[UIImageLayer alloc] initWithFrame:CGRectMake(inWidth, inHeight, self.frame.size.width-2*inWidth, self.frame.size.height-2*inHeight)];
    innerImageView.sendWebMsg = TRUE;
    [innerImageView initImage:link useOriginalSize:FALSE ratioMode:1 layerInfo:Nil ]; 
    [self addSubview:innerImageView];
    
    CGRect innerImageFrame = [innerImageView getInnerImageFrame];
    bgView.frame = CGRectMake((self.frame.size.width-innerImageFrame.size.width)/2-inWidth, (self.frame.size.height-innerImageFrame.size.height)/2-inHeight,
                                                     innerImageFrame.size.width+2*inWidth, innerImageFrame.size.height+2*inHeight) ;
    
    fontSize = inFontSize;
    titleTextfield = [[UITextField alloc] initWithFrame:CGRectMake(0,bgView.frame.origin.y+bgView.frame.size.height,
                                                                                                        self.frame.size.width,inFontSize+3)];
    titleTextfield.textAlignment = UITextAlignmentCenter;
    titleTextfield.enabled = false;
    titleTextfield.borderStyle = UITextBorderStyleNone;//UITextBorderStyleRoundedRect;
    titleTextfield.textColor = [UIColor whiteColor]; //text color
    titleTextfield.font = [UIFont systemFontOfSize:inFontSize];  //font size   
    titleTextfield.backgroundColor =[UIColor clearColor];
    titleTextfield.autocorrectionType = UITextAutocorrectionTypeNo;	// no auto correction support   
    titleTextfield.text = inTitle;
    [self addSubview:titleTextfield];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // assign a UITouch object to the current touch
    UITouch *touch = [[event allTouches] anyObject];
    
    //single tap detect
    if ([touch view] == innerImageView ) 
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"category_unit_init" object:self];
    }

}

-(void) layoutAdjust
{
    if (bgView != Nil && titleTextfield != Nil)
    {
        CGRect innerImageFrame = [innerImageView getInnerImageFrame];
        
        bgView.frame = CGRectMake((self.frame.size.width-innerImageFrame.size.width)/2-borderWidth, 
                                  (self.frame.size.height-innerImageFrame.size.height)/2-borderHeight,
                                   innerImageFrame.size.width+2*borderWidth, 
                                   innerImageFrame.size.height+2*borderHeight) ;
        
        titleTextfield.frame = CGRectMake(0,bgView.frame.origin.y+bgView.frame.size.height,
                                                                self.frame.size.width,fontSize+3);
    }

}

- (void) reset
{
    if (bgView != Nil) 
    {
        [bgView removeFromSuperview];
        [bgView release];
    }
    
    if (innerImageView != Nil) 
    {
        [innerImageView reset];
        [innerImageView removeFromSuperview];
        [innerImageView release];
    }
    
    if (titleTextfield != Nil) 
    {
        [titleTextfield removeFromSuperview];
        [titleTextfield release];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
